package com.getpebble.android.framework.protocol.inbound;

import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FirmwareVersion;
import com.getpebble.android.common.version.FirmwareVersionInfo;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.getpebble.android.framework.util.PebbleCapabilities;
import com.google.common.primitives.UnsignedInteger;
import java.nio.ByteBuffer;

public class PblInboundVersionMessage extends PblInboundMessage {
    public static final String TAG = PblInboundVersionMessage.class.getSimpleName();
    private byte[] mBluetoothDeviceAddress;
    private UnsignedInteger mBootLoaderTimestamp;
    private String mHardwareRevision;
    private String mLanguageLocale;
    private UnsignedInteger mLanguageVersion;
    private FirmwareVersionInfo mNormalFirmwareVersion;
    private FirmwareVersionInfo mSafeFirmwareVersion;
    private String mSerialNumber;

    public class InvalidVersionMessageException extends RuntimeException {
        public InvalidVersionMessageException(String message) {
            super(message);
        }
    }

    public PblInboundVersionMessage(ProtocolMessage message) {
        super(message);
        ByteBuffer buffer = message.getDataBuffer();
        byte format = buffer.get();
        this.mNormalFirmwareVersion = getFirmwareVersionInfoFromBuffer(buffer);
        if (this.mNormalFirmwareVersion == null) {
            throw new InvalidVersionMessageException("Normal Firmware version is null!");
        }
        this.mSafeFirmwareVersion = getFirmwareVersionInfoFromBuffer(buffer);
        this.mBootLoaderTimestamp = ByteUtils.getUint32FromBuffer(buffer);
        this.mHardwareRevision = ByteUtils.getStringFromBuffer(buffer, 9);
        this.mSerialNumber = ByteUtils.getStringFromBuffer(buffer, 12);
        this.mBluetoothDeviceAddress = flipByteOrder(ByteUtils.getBytesFromBuffer(buffer, 6));
        ByteUtils.getUint32FromBuffer(buffer);
        ByteUtils.getUint32FromBuffer(buffer);
        if (PebbleCapabilities.remoteSupportsLanguagePacks(this.mNormalFirmwareVersion.getFirmwareVersion())) {
            this.mLanguageLocale = ByteUtils.getStringFromBuffer(buffer, 6);
            this.mLanguageVersion = ByteUtils.getUint16FromBuffer(buffer);
            Trace.debug(TAG, String.format("Found a Pebble with locale %s and version %d", new Object[]{this.mLanguageLocale, Integer.valueOf(this.mLanguageVersion.intValue())}));
            return;
        }
        Trace.debug(TAG, "Remote does not support language packs; version: " + this.mNormalFirmwareVersion.getFirmwareVersion());
    }

    public static byte[] flipByteOrder(byte[] bytes) {
        int left = 0;
        for (int right = bytes.length - 1; left < right; right--) {
            byte temp = bytes[left];
            bytes[left] = bytes[right];
            bytes[right] = temp;
            left++;
        }
        return bytes;
    }

    private FirmwareVersionInfo getFirmwareVersionInfoFromBuffer(ByteBuffer buffer) {
        UnsignedInteger timestamp = ByteUtils.getUint32FromBuffer(buffer);
        FirmwareVersion firmwareVersion = null;
        try {
            firmwareVersion = new FirmwareVersion(ByteUtils.getStringFromBuffer(buffer, 32), timestamp.longValue());
        } catch (Exception e) {
            Trace.warning(TAG, "error parsing firmware version");
        }
        String gitCommit = ByteUtils.getStringFromBuffer(buffer, 8);
        boolean isRecoveryFirmware = ByteUtils.getBooleanFromBuffer(buffer);
        UnsignedInteger hardwarePlatform = ByteUtils.getUint8FromBuffer(buffer);
        return new FirmwareVersionInfo().setTimestamp(timestamp.longValue()).setFirmwareVersion(firmwareVersion).setGitCommit(gitCommit).setIsRecovery(isRecoveryFirmware).setHardwarePlatform(hardwarePlatform).setMetadataVersion(ByteUtils.getUint8FromBuffer(buffer));
    }

    public FirmwareVersionInfo getNormalFirmwareVersionInfo() {
        return this.mNormalFirmwareVersion;
    }

    public FirmwareVersionInfo getSafeFirmwareVersionInfo() {
        return this.mSafeFirmwareVersion;
    }

    public String getHardwareRevision() {
        return this.mHardwareRevision;
    }

    public String getSerialNumber() {
        return this.mSerialNumber;
    }

    public EndpointId getId() {
        return EndpointId.VERSIONS;
    }

    protected int getMinSize() {
        return 126;
    }

    public String getLanguageLocale() {
        return this.mLanguageLocale;
    }

    public int getLanguageVersion() {
        return this.mLanguageVersion == null ? 0 : this.mLanguageVersion.intValue();
    }
}
